package com.vilen.weather.v_learn_firstcode.app.L_3;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.vilen.weather.v_learn_firstcode.app.BaseActivity;
import com.vilen.weather.v_learn_firstcode.app.R;
import com.vilen.weather.v_learn_firstcode.app.adapter.MsgAdapter;
import com.vilen.weather.v_learn_firstcode.app.model.Msg;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by vilen on 2015/5/28.
 */
public class UIBestPracticeActivity extends BaseActivity{
    private ListView msgListView;
    private EditText inputText;
    private Button send;
    private MsgAdapter adapter;
    private List<Msg> msgList = new ArrayList<Msg>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_ui);
        initMsgs();
        adapter = new MsgAdapter(UIBestPracticeActivity.this,R.layout.msg_item,msgList);
        inputText = (EditText) findViewById(R.id.input_text);
        send = (Button) findViewById(R.id.send);
        msgListView = (ListView) findViewById(R.id.msg_list_view);
        msgListView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = inputText.getText().toString();
                if (!"".equals(content)) {
                    Msg msg = new Msg(content,Msg.TYPE_SENT);
                    msgList.add(msg);
                    adapter.notifyDataSetChanged();
                    msgListView.setSelection(msgList.size());//listview定位到最后一行
                    inputText.setText("");//清空输入框
                }
            }
        });
    }

    private void initMsgs() {
        Msg msg1 = new Msg("Hello guy.",Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Hello. Who is that?",Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tom,Nice talking to you.",Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }

    public static void actionStart(Context context,String data1,String data2) {
        Intent intent = new Intent(context,UIBestPracticeActivity.class);
        intent.putExtra("param1",data1);
        intent.putExtra("param2",data2);
        context.startActivity(intent);
    }
}
